//
//  NetWork.swift
//  NetWork
//
//  Created by Delin Meng 孟德林 on 2018/7/31.
//  Copyright © 2018年 Delin Meng 孟德林. All rights reserved.
//

import Foundation

public protocol URLConvertible {
    
    func asURL() throws -> URL
    
}

extension URLRequest {
    
    init(url:URLConvertible,method: HTTPMethod,headers:HTTPHeaders? = nil) throws {
        
        let url = try url.asURL()
        self.init(url: url)
        httpMethod = method.rawValue
        if let headers = headers {
            for (headerFiled , headerValue) in headers {
                setValue(headerValue, forHTTPHeaderField: headerFiled)
            }
        }
    }
    
    func adapt(using adapter: RequestAdapter?) throws -> URLRequest {
        guard let adapter = adapter else { return self }
        return try adapter.adapt(self)
    }
    
}






